package com.jiemengshi.server.service;

import com.jiemengshi.server.mapper.FormIdMapper;
import com.jiemengshi.server.mapper.UserWechatMapper;
import com.jiemengshi.server.model.FormId;
import com.jiemengshi.server.model.UserWechat;
import com.jiemengshi.server.util.DateUtil;
import com.jiemengshi.server.util.ErrorCodeMsg;
import com.jiemengshi.server.util.PrjException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by MingQi on 2017/10/14.
 */
@Service
public class WechatUserService {
    @Autowired
    private UserWechatMapper userWechatMapper;

    @Autowired
    private FormIdMapper formIdMapper;
    
    @Autowired
    private WechatService wechatService;

    public boolean userWechatLogin(HttpServletRequest request, String code, UserWechat userWechat, String appName) throws PrjException {
        String openId = wechatService.getOpenIdByCode(code, appName);

        int userId;
        UserWechat userSaved = getUserWechatByOpenId(openId);
        if (userSaved != null) {
            userWechat.setId(userSaved.getId());
            userWechat.setLastLogin(DateUtil.getCurrentTime());
            userWechat.setCreateTime(userSaved.getCreateTime());
            userWechat.setOpenId(openId);
            userWechat.setLevel(userSaved.getLevel());
            
            userWechatMapper.updateByPrimaryKeySelective(userWechat);

            userId = userSaved.getId();
        } else {
            userWechat.setOpenId(openId);
            userWechat.setCreateTime(DateUtil.getCurrentTime());
            userWechat.setLastLogin(DateUtil.getCurrentTime());
            
            addUserWechat(userWechat);

            userId = userWechat.getId();
        }

        request.getSession().setAttribute("userInfo", userId + "_" + openId);
        return true;
    }
    
    public boolean addUserWechat(UserWechat userWechat) {
        userWechat.setCreateTime(DateUtil.getCurrentTime());
        userWechat.setLastLogin(DateUtil.getCurrentTime());
        return userWechatMapper.insertSelective(userWechat) > 0;
    }

    public UserWechat getUserWechatByOpenId(String wechatOpenId) {
        return userWechatMapper.selectByOpenId(wechatOpenId);
    }

    public UserWechat getUserWechatById(int id) {
        return userWechatMapper.selectByPrimaryKey(id);
    }
    
    public int getLoginUserId(HttpServletRequest request) throws PrjException {
        Object sUserInfo = request.getSession().getAttribute("userInfo");

        if(sUserInfo == null)
            throw new PrjException(ErrorCodeMsg.USER_WECHAT_NOT_LOGIN);

        return Integer.parseInt(((String)sUserInfo).split("_")[0]);
    }

    public String getLoginUserWechatOpenId(HttpServletRequest request) throws PrjException {
        Object sUserInfo = request.getSession().getAttribute("userInfo");

        if(sUserInfo == null)
            throw new PrjException(ErrorCodeMsg.USER_WECHAT_NOT_LOGIN);

        return ((String)sUserInfo).split("_")[1];
    }

    public UserWechat getLoginUserWechat(HttpServletRequest request) throws PrjException {
        Object sUserInfo = request.getSession().getAttribute("userInfo");

        if(sUserInfo == null)
            throw new PrjException(ErrorCodeMsg.USER_WECHAT_NOT_LOGIN);

        return getUserWechatById(Integer.parseInt(((String) sUserInfo).split("_")[0]));
    }

    public boolean updateUserWechat(UserWechat userWechat) {
        return userWechatMapper.updateByPrimaryKeySelective(userWechat) > 0;
    }

    public void addFormId(int userId, String formId) {
        FormId entity = new FormId();
        entity.setFormId(formId);
        entity.setUserId(userId);
        entity.setCreateTime(DateUtil.getCurrentTime());
        
        formIdMapper.insertSelective(entity);
    }

    public String consumeFormId(int userId) {
        FormId formId = formIdMapper.selectOneByUserId(userId);
        if(formId == null)
            return "";

        formIdMapper.deleteByPrimaryKey(formId.getId());
        return formId.getFormId();
    }
}
